package com.qcby.controller;


import com.qcby.entity.Course;
import com.qcby.entity.Teacher;
import com.qcby.entity.TeacherPlanCourse;
import com.qcby.service.CourseService;
import com.qcby.service.TeacherPlanCourseService;
import com.qcby.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherPlanCourseService teacherPlanCourseService;



    /**
     * 登录
     * @return
     */
    @RequestMapping("t_login")
    public String t_login(Integer msg,Model model){
        //传回来的msg=5是Integer类型的要是是字符串类型应是msg='5'
        System.out.println(msg);
        if (msg==null){//空指针的处理
            return "t_login";
        }
        if(msg==5){
            //System.out.println("我等于5");
            model.addAttribute("msg",5);
            return "t_login";
        }
        return "t_login";
    }
    /**
     * 检测输入账户是否正确/正确进入主页面/错误就回到登陆界面
     * @return
     */
    @RequestMapping("test_login")
    public String test_login(Teacher teacher,Model model,HttpServletRequest request) {
        List<Teacher>  a = teacherService.find(teacher);
      //  System.out.println(a);
        if(a != null && !a.isEmpty()){
            HttpSession session = request.getSession();
            session.setAttribute("tea",a);
           model.addAttribute("teacher",a.get(0));
            //成功进入主页面
            return "t_homePage";
        }else{
            System.out.println(a);
            //想写一个弹出窗
            model.addAttribute("msg",1);
            return "t_login";
        }
    }
    //进入选课页面
    @RequestMapping("course")
    public String course(Course course, Model model,HttpSession session){
        List<Teacher> teachers = (List<Teacher>) session.getAttribute("tea");//获取session的数据
        //System.out.println(teachers);
        model.addAttribute("teacher",teachers.get(0));//因为传进来是一个List数组,要获取这个数组里面
        List<Course> listAll =  courseService.listAll(course);
        model.addAttribute("listDatas",listAll);
        return "course";
    }
    //添加
    @RequestMapping("addTo")
    public String addTo(Integer course,String time,Model model,Course courses,HttpSession session){//疑问为什么用Teacher teacher接收不到
        List<Teacher> tea = (List<Teacher>) session.getAttribute("tea");
        //System.out.println("===="+tea);//获取的是一个数组
        Integer teacher_id = tea.get(0).gettea_account();
        System.out.println("教工号" +teacher_id);
        System.out.println("课程号" +course);//获取到课程编号
        System.out.println("上课时间"+ time);//获取到上课时间
        //是查看是否已经选过这个课
        int i = teacherPlanCourseService.find(teacher_id, course);//没有获取到就返回0获取到的就返回1
        if(i==0){//如果没有存过该条数据
            //System.out.println("我被加进去啦");
            //在这里再判断时间是否冲突
            //查询这个老师已选的课里有没有这个时间的
            Integer t = teacherPlanCourseService.findTime(time,teacher_id);
            if(t==0){//证明没有过这个时间段的课
                teacherPlanCourseService.insert(course, teacher_id);//那么正常加入进去即可
            }
            //System.out.println("这次要加入的上课时间是否存在"+t);
            else{
                model.addAttribute("msg",4);//你这个时间段已经选过别的课
                model.addAttribute("listDatas",courseService.listAll(courses));
                return "course";
            }
        }else{
            //System.out.println("没有加入我");
            model.addAttribute("msg",3);//你已经选过此课
            //这里有个坑!!!
            model.addAttribute("listDatas",courseService.listAll(courses));
            return "course";
        }
        return "redirect:course";
    }
    //进入选课结果界面
    @RequestMapping("teacherResult")
    public String teacherCourse(HttpSession session,Model model){
        List<Teacher> teachers = (List<Teacher>) session.getAttribute("tea");
        int teacher_id = teachers.get(0).gettea_account();//获取教师号
       // System.out.println("教师id"+teacher_id);
        List<TeacherPlanCourse> teacherPlanCourse = teacherPlanCourseService.selectByPrimaryKey(teacher_id);
        //System.out.println("查表里有没有该老师选的课"+teacherPlanCourse);
        //这里返回的是[]空数组不能用null判断,小坑
        if(teacherPlanCourse.isEmpty()){//如果教师没有选课的时候,那么上面查询teacher_id返回'空值',那么下面再用的时候会报空指针
            //System.out.println("目前我没有选课");
            return "teacherResult";
        }
        //System.out.println("=========");
        //System.out.println(teacherPlanCourse);
        List<Integer> a = new ArrayList<>();
        for (int i=0;i <= teacherPlanCourse.size()-1;i++){
            Integer planCourse_id = teacherPlanCourse.get(i).getPlanCourse_id();
            System.out.println("课程号"+planCourse_id);
            a.add(planCourse_id);
        }
        //System.out.println(a);
        List<Course> list = courseService.list(a);
        //System.out.println(list);
        model.addAttribute("list",list);
        return "teacherResult";
    }
    //查询功能
    @RequestMapping("find")
    public String find(String courseIdName,Model model,Course course){
        //System.out.println(courseIdName);//测试是否获取到了
        List<Course> courses = courseService.find(courseIdName);
        //System.out.println(courses);//看输出是什么
        if (courses.isEmpty()){//如果为空,证明查的东西没有
            model.addAttribute("msg",2);//往页面传一个值,说没获取到
            model.addAttribute("listDatas",courseService.listAll(course));
            return "course";
        }
        model.addAttribute("listDatas",courses);
        return "course";
    }
    //删除
    @RequestMapping("deletebyid")
    public String deletebyid(Integer no,Model model){
        System.out.println(no);//检测是否获取到了课程号
        int i = teacherPlanCourseService.delete(no);
        return "redirect:teacherResult";
    }
    //修改密码
    @RequestMapping("changePassword")
    public String changePassword(String password, Model model, HttpSession session){
        List<Teacher> teachers = (List<Teacher>) session.getAttribute("tea");
        model.addAttribute("teacher",teachers.get(0));
        Integer tea_account =teachers.get(0).gettea_account();//获取教师id
        System.out.println("获取的密码"+password);
        if(password==null){
            return "changePassword";
        }else{
           Integer i = teacherService.changPassword(password,tea_account);
           //attr.addAttribute("msg",5);//这个是带参数的重定向,自动拼接url
           //System.out.println(i);
           // 修改成功之后回到登录页面
           model.addAttribute("msg",6);
           //return "redirect:t_login
            return "changePassword";
        }
    }

}
